import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;


public class MsgProducer {

	public static void main(String[] args) throws Exception {
			
		Properties properties = new Properties();
		
        //指定消息key序列化方式
        properties.put("key.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        //指定消息本身的序列化方式
        properties.put("value.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        //设置消息的发送地址
        properties.put("bootstrap.servers", "localhost:9092");

        //键和值都是String类型
        Producer<String, String> myProducer = new KafkaProducer<String, String>(properties);
        try
        {
        	myProducer.send(new ProducerRecord<String, String>("myQueue", "msgKey", "Hello Kafka"));
            System.out.println("Msg sent successfully");	
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }
        finally{
        	if(myProducer != null){
        		myProducer.close();	
        	}    
        	properties.clear();
        }		
	}
}
